package com.hengxinguotong.zhihuichengjian.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.bean.MeasurePoint;
import com.hengxinguotong.zhihuichengjian.ui.WebViewActivity;
import com.hengxinguotong.zhihuichengjian.utils.Utils;
import com.hengxinguotong.zhihuichengjian.widget.font.HXEditTextView;
import com.hengxinguotong.zhihuichengjian.widget.font.HXTextView;
import com.hengxinguotong.zhihuichengjian.widget.view.KeyboardPop;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

import static cn.jpush.android.api.JPushInterface.a.c;
import static cn.jpush.android.api.JPushInterface.a.i;
import static cn.jpush.android.api.JPushInterface.a.m;

/**
 * Created by Administrator on 2017/12/20.
 */

public class MeasurePointAdapter extends BaseAdapter {
    private List<MeasurePoint> datas;
    private Activity activity;
    private LinearLayout mainLl;
    private KeyboardPop pop;
    private int type;//0查看详情 1输入数据；

    public MeasurePointAdapter(List<MeasurePoint> datas, Activity activity, int type, LinearLayout mainLl) {
        this.datas = datas;
        this.activity = activity;
        this.mainLl = mainLl;
        this.type = type;
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return datas.get(i);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if(null == convertView || null == convertView.getTag()){
            convertView = LayoutInflater.from(activity).inflate(R.layout.item_measure_point, parent ,false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        final MeasurePoint measurePoint = datas.get(position);
        holder.measurePointNameTv.setText(measurePoint.getName());
        holder.positionStandardTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(activity, WebViewActivity.class);
                intent.putExtra("url", "file:///android_asset/measure_standard.html");
                intent.putExtra("title", "检查标准");
                intent.putExtra("measurePoint", measurePoint);
                activity.startActivity(intent);
            }
        });
        if(type == 0){//0查看详情 1输入数据；
            Utils.setEditTextViewUnable(measurePoint.getTotalQty() + "", holder.checkedCountEtv);
            Utils.setEditTextViewUnable(measurePoint.getPassQty() + "", holder.passedCountEtv);
        }else if(type == 1){
            if("Meizu".equals(Build.BRAND)){
                holder.checkedCountEtv.setOnTouchListener(new View.OnTouchListener() {//然后再listview适配器的getview的时候设置触摸方法
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        if (event.getAction() == MotionEvent.ACTION_UP) {
                            pop = new KeyboardPop(activity, R.layout.my_keyboard, holder.checkedCountEtv);
                            pop.setFocusable(true);
                            pop.setOutsideTouchable(true);
                            pop.setBackgroundDrawable(activity.getResources().getDrawable(R.color.color_transparent));
                            pop.update();
                            pop.setOnDismissListener(new PopupWindow.OnDismissListener() {
                                @Override
                                public void onDismiss() {

                                }
                            });
                            if (pop.isShowing()) {
                                pop.dismiss();
                            } else {
                                pop.showAtLocation(mainLl, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0); //设置layout在PopupWindow中显示的位置
                            }
                        }
                        return false;
                    }
                });
                holder.passedCountEtv.setOnTouchListener(new View.OnTouchListener() {//然后再listview适配器的getview的时候设置触摸方法
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        if (event.getAction() == MotionEvent.ACTION_UP) {
                            pop = new KeyboardPop(activity, R.layout.my_keyboard, holder.passedCountEtv);
                            pop.setFocusable(true);
                            pop.setOutsideTouchable(true);
                            pop.setBackgroundDrawable(activity.getResources().getDrawable(R.color.color_transparent));
                            pop.update();
                            pop.setOnDismissListener(new PopupWindow.OnDismissListener() {
                                @Override
                                public void onDismiss() {

                                }
                            });
                            if (pop.isShowing()) {
                                pop.dismiss();
                            } else {
                                pop.showAtLocation(mainLl, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0); //设置layout在PopupWindow中显示的位置
                            }
                        }
                        return false;
                    }
                });
            }
        }
        return convertView;
    }

    static class ViewHolder{
        @BindView(R.id.measure_point_name_tv)
        HXTextView measurePointNameTv;
        @BindView(R.id.position_standard_tv)
        HXTextView positionStandardTv;
        @BindView(R.id.checked_count_etv)
        HXEditTextView checkedCountEtv;
        @BindView(R.id.passed_count_etv)
        HXEditTextView passedCountEtv;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
